package com.yungu.swift.utils;

import java.util.Calendar;
import java.util.Date;

/**
 * 时间-日历 工具类
 *
 * @author cuixiuyin
 */
public class CalendarUtil {

    /**
     * 此工具将Calendar中月份在此调整按1开始，而不是从0开始
     */

    static Calendar calendar = Calendar.getInstance();

    /**
     * 获取年
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取月
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取周几（工作日）
     *
     * @param date
     * @return
     */
    public static int getDayOfWeek(Date date) {
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    /**
     * 获取日
     *
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取时
     */
    public static int getHours(Date date) {
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取分
     *
     * @param date
     * @return
     */
    public static int getMinutes(Date date) {
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 获取秒
     *
     * @param date
     * @return
     */
    public static int getSeconds(Date date) {
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }

    /**
     * 设置年
     *
     * @param date
     * @param year
     * @return
     */
    public static Date setYear(Date date, int year) {
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, year);
        return calendar.getTime();
    }

    /**
     * 设置月
     *
     * @param date
     * @param month
     * @return
     */
    public static Date setMonth(Date date, int month) {
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, month - 1);
        return calendar.getTime();
    }

    /**
     * 设置日
     *
     * @param date
     * @param day
     * @return
     */
    public static Date setDay(Date date, int day) {
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }

    /**
     * 设置时
     *
     * @param date
     * @param hour
     * @return
     */
    public static Date setHours(Date date, int hour) {
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        return calendar.getTime();
    }

    /**
     * 设置分
     *
     * @param date
     * @param minute
     * @return
     */
    public static Date setMinutes(Date date, int minute) {
        calendar.setTime(date);
        calendar.set(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    /**
     * 设置秒
     *
     * @param date
     * @param second
     * @return
     */
    public static Date setSeconds(Date date, int second) {
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, second);
        return calendar.getTime();
    }

    /**
     * 年份加
     *
     * @param date
     * @param plusValue
     * @return
     */
    public static Date yearPlus(Date date, int plusValue) {
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, getYear(date) + plusValue);
        return calendar.getTime();
    }

    /**
     * 月份加
     *
     * @param date
     * @param plusValue
     * @return
     */
    public static Date monthPlus(Date date, int plusValue) {
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, (getMonth(date) - 1) + plusValue);
        return calendar.getTime();
    }

    /**
     * 天数加
     *
     * @param date
     * @param plusValue
     * @return
     */
    public static Date dayPlus(Date date, int plusValue) {
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, getDay(date) + plusValue);
        return calendar.getTime();
    }

    /**
     * 小时数加
     *
     * @param date
     * @param plusValue
     * @return
     */
    public static Date hoursPlus(Date date, int plusValue) {
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, getHours(date) + plusValue);
        return calendar.getTime();
    }

    /**
     * 分钟数加
     *
     * @param date
     * @param plusValue
     * @return
     */
    public static Date minutesPlus(Date date, int plusValue) {
        calendar.setTime(date);
        calendar.set(Calendar.MINUTE, getMinutes(date) + plusValue);
        return calendar.getTime();
    }

    /**
     * 秒数加
     *
     * @param date
     * @param plusValue
     * @return
     */
    public static Date sencondsPlus(Date date, int plusValue) {
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, getSeconds(date) + plusValue);
        return calendar.getTime();
    }


    /**
     * 年份减
     *
     * @param date
     * @param plusValue
     * @return
     */
    public static Date yearMinus(Date date, int plusValue) {
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, getYear(date) - plusValue);
        return calendar.getTime();
    }

    /**
     * 月份减
     *
     * @param date
     * @param plusValue
     * @return
     */
    public static Date monthMinus(Date date, int plusValue) {
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, (getMonth(date) - 1) - plusValue);
        return calendar.getTime();
    }

    /**
     * 天数减
     *
     * @param date
     * @param plusValue
     * @return
     */
    public static Date dayMinus(Date date, int plusValue) {
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, getDay(date) - plusValue);
        date = calendar.getTime();
        return calendar.getTime();
    }

    /**
     * 小时数减
     *
     * @param date
     * @param plusValue
     * @return
     */
    public static Date hoursMinus(Date date, int plusValue) {
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, getHours(date) - plusValue);
        return calendar.getTime();
    }

    /**
     * 分钟数减
     *
     * @param date
     * @param plusValue
     * @return
     */
    public static Date minutesMinus(Date date, int plusValue) {
        calendar.setTime(date);
        calendar.set(Calendar.MINUTE, getMinutes(date) - plusValue);
        return calendar.getTime();
    }

    /**
     * 秒数减
     *
     * @param date
     * @param plusValue
     * @return
     */
    public static Date sencondsMinus(Date date, int plusValue) {
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, getSeconds(date) - plusValue);
        return calendar.getTime();
    }

}
